//
//  LocationHistoryDatePickerView.m
//  EZ Tracker
//
//  Created by FENGYL on 2016/11/30.
//  Copyright © 2016年 FENGYL. All rights reserved.
//

#import "LocationHistoryDatePickerView.h"
#import "UIView+XL.h"

@interface LocationHistoryDatePickerView()

@property (strong , nonatomic) UIDatePicker * datePicker;

@end

@implementation LocationHistoryDatePickerView

- (instancetype)initWithLocationHistoryDatePickerViewStyle:(BOOL)isDate dateString:(NSString *)dateString
{
    self = [super init];
    if (self) {
        
        self.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 290, [UIScreen mainScreen].bounds.size.width, 290);
        
        [self setBackgroundColor:[UIColor whiteColor]];
        
        [self setViewShadow:self];
        
        
        UIToolbar * toolbar = [self setUptoolBar];
        
        
        self.datePicker = [self setUpDatePickerWithToolBarHeight:toolbar.height isDate:isDate dateString:dateString];
        
        
        [self addSubview:toolbar];
        [self addSubview:self.datePicker];
        
    }
    return self;
}

#pragma mark datePicker
-(UIDatePicker *)setUpDatePickerWithToolBarHeight:(CGFloat)toolbarHeight isDate:(BOOL)isDate dateString:(NSString *)dateString{
    
    UIDatePicker * datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height - toolbarHeight)];
    
    datePicker.y = (self.height - toolbarHeight - datePicker.height)/2 + toolbarHeight;
    
    
    if (isDate) {
        
        //年月日
        datePicker.datePickerMode = UIDatePickerModeDate;
        
        //设置datepicker当前选择时间
        
        NSString * year = [dateString substringWithRange:NSMakeRange(6, 4)];
        
        NSString * month = [dateString substringWithRange:NSMakeRange(3, 2)];
        
        NSString * day = [dateString substringToIndex:2];
        
        NSLog(@"%@ %@ %@",year,month,day);
        
        NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
        
        formatter.dateFormat = @"yyyy-MM-dd";
        
        NSDate * date = [formatter dateFromString:[NSString stringWithFormat:@"%@-%@-%@",year,month,day]];
        
        [datePicker setDate:date];
        
    }else{
        
        datePicker.datePickerMode = UIDatePickerModeTime;
        
        
        NSString * hour = [dateString substringToIndex:2];
        
        NSString * minute = [dateString substringWithRange:NSMakeRange(3, 2)];
        
        NSLog(@"%@ %@",hour,minute);
        
        NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
        
        formatter.dateFormat = @"HH:mm";
        
        NSDate * date = [formatter dateFromString:[NSString stringWithFormat:@"%@:%@",hour,minute]];
        
        NSLog(@"date %@",date);
        
        [datePicker setDate:date];
        
    }
    
    
    
    return datePicker;
    
}

#pragma mark toolBar
-(UIToolbar *)setUptoolBar{
    
    UIToolbar * toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.width, 40)];
    
    
    UIBarButtonItem * flexibleItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    
    
    UIBarButtonItem * cancelButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleDone target:self action:@selector(touchCancelButtonItem)];
    
    
    UIBarButtonItem * fixedItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
    
    
    UIBarButtonItem * confirmButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Confirm" style:UIBarButtonItemStyleDone target:self action:@selector(touchConfirmButtonItem)];
    [confirmButtonItem setTintColor:[UIColor redColor]];
    
    
    NSArray * itemArray = [NSArray arrayWithObjects:flexibleItem,cancelButtonItem,fixedItem,confirmButtonItem, nil];
    
    
    [toolbar setItems:itemArray];
    
    return toolbar;
    
}

#pragma mark Action for cancelButton
-(void)touchCancelButtonItem{
    
    self.removeDatePickerView();
    
}

#pragma mark Action for confirmButton
-(void)touchConfirmButtonItem{
    
    NSDate * date = self.datePicker.date;
    
    self.selectDate(date);
    
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd/MM/yyyy"];
    NSString *dateString = [dateFormatter stringFromDate:date];

    
    NSDateFormatter * dateformatter = [[NSDateFormatter alloc]init];
    [dateformatter setDateFormat:@"hh:mm aa"];
    NSString * datestring = [dateformatter stringFromDate:date];
    
    NSLog(@"%@ %@",dateString,datestring);
    
}

#pragma mark View阴影效果
-(void)setViewShadow:(UIView *)sender{
    
    sender.layer.shadowColor = [UIColor blackColor].CGColor;
    
    sender.layer.shadowOffset = CGSizeMake(2, 2);
    
    sender.layer.shadowOpacity = 0.4;
    
    sender.layer.shadowRadius = 4;
    
}

@end
